package com.zhitong.money.controller;

import com.github.pagehelper.PageInfo;
import com.zhitong.money.entity.AjaxResponse;
import com.zhitong.money.entity.Permission;
import com.zhitong.money.services.api.IPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@Slf4j
@RestController
public class PermissionContro {

    @Autowired
    private IPermissionService service;

    @GetMapping("/permissions")
    public AjaxResponse getAllPermission() throws Exception{
        List<Permission> allPermission = service.findAllPermission();
        return AjaxResponse.success(allPermission);
    }

    @GetMapping("/permission/{id}")
    public AjaxResponse getPermissionById(@PathVariable String id) throws Exception{
        Permission PermissionById = service.findPermissionById(id);
        return AjaxResponse.success(PermissionById);
    }

    @GetMapping("/permissionRid/{rid}")
    public AjaxResponse getPermissionByRid(@PathVariable String rid) throws Exception{
        List<Permission> persByRid = service.findPersByRid(rid);
        return AjaxResponse.success(persByRid);
    }

    @RequiresPermissions({"addPermission"})
    @PostMapping("/permission")
    public AjaxResponse addPermission(@RequestBody Permission Permission) throws Exception{
        service.addPermission(Permission);
        return AjaxResponse.success("添加资源成功");

    }

    @PostMapping("/permissionMap")
    public AjaxResponse getPermissionByMap(@RequestBody Map<String,Object> map) throws Exception{
        List<Permission> resByMap = service.findPerByMap(map);
        return AjaxResponse.success(resByMap);
    }

    @PostMapping("/permissions")
    public AjaxResponse getPermissionByPage(@RequestBody Map<String,Object> map) throws Exception{
        PageInfo allByMap = service.findAllByPage(map);
        return AjaxResponse.success(allByMap);
    }

    @RequiresPermissions({"updatePermission"})
    @PutMapping("/permission")
    public AjaxResponse updatePermission(@RequestBody Permission Permission) throws Exception{
        service.updatePermission(Permission);
        return AjaxResponse.success("跟新资源成功!");
    }

    @RequiresPermissions({"delPermission"})
    @DeleteMapping("/permission/{id}")
    public AjaxResponse delPermission(@PathVariable("id")String id) throws Exception{
        int i = service.delPermission(id);
        if (i>0){
            return AjaxResponse.success("删除资源成功!");
        }
        return AjaxResponse.fail("删除资源失败!资源被关联");

    }

    @RequiresPermissions({"delPermission"})
    @PostMapping("/delPermissions")
    public AjaxResponse delPermissionByIds(@RequestBody List<String> ids) throws Exception{
        int i = service.delPermissionByIds(ids);
        if (i>0){
            return AjaxResponse.success("删除资源成功!");
        }
        return AjaxResponse.fail("删除资源失败!资源被关联");
    }

}
